package com.m3958.firstgwt.client.nativemethod;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.smartgwt.client.util.SC;

public class NativeUtils {
	
	@Inject
	private EventBus eventBus;
	
	
	public JsArrayString getJsArrayString(String...ss){
		JsArrayString jsas = JsArrayString.createArray().cast();
		for(String s : ss){
			jsas.push(s);
		}
		return jsas;
	}
	
		private void showIndicator(String s){
			if(s == null || s.isEmpty()){
				SC.showPrompt("");
			}else{
				SC.showPrompt(s);
			}
		}
		
		private void hideIndicator(){
			SC.clearPrompt();
		}
	
//	public void loadScriptViaCallBack(NativeUtils en,String scriptUrl,String scriptsName,String indicatorStr,IwantLoadScripts scriptReceiver){
//		JsArrayString scriptUrls = JsArrayString.createArray().cast();
//		scriptUrls.push(scriptUrl);
//		loadScriptsViaCallback(en, scriptUrls, scriptsName, indicatorStr,scriptReceiver);
//	}
//	
//	public void scriptLoadCallback(IwantLoadScripts scriptReceiver,boolean success){
//		if(success){
//			scriptReceiver.onScriptLoadSuccess();
//		}else{
//			scriptReceiver.onScriptLoadFailure();
//		}
//	}
//	
//	 public final native void loadScriptsViaCallback(NativeUtils en,JsArrayString scriptUrls,String scriptsName,String indicatorStr,IwantLoadScripts scriptReceiver)/*-{
//	 	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::showIndicator(Ljava/lang/String;)(indicatorStr);
//	    $wnd.Y.Get.script(scriptUrls,
//		  {
//		    onSuccess: function(o) {
//				en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadScripts;Z)(scriptReceiver,true);
//				en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::hideIndicator()();
//		    },
//		    onFailure: function(o) {
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadScripts;Z)(scriptReceiver,false);
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::hideIndicator()();
//		      $wnd.alert(scriptsName + "加载失败！");
//		    },
//		    onTimeout: function(o) {
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadScripts;Z)(scriptReceiver,false);
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::hideIndicator()();
//		      $wnd.alert(scriptsName + "加载超时！");
//		    },
//		    data: "foo",
//		    timeout: 10000, // 10 second timeout
//		    context: $wnd//, // make the YUI instance
//		    // win: otherframe // target another window/frame
//		    //autopurge: true // allow the utility to choose when to
//		                    // remove the nodes
//		    //purgetheshold: 1 // purge previous transaction before
//		                     // next transaction
//		  });
//	}-*/;
//	 
//	 public void loadCss(NativeUtils en,String cssUrl,String csssName,String indicatorStr,IwantLoadScripts cssReceiver){
//		 JsArrayString cssUrls = JsArrayString.createArray().cast();
//		 cssUrls.push(cssUrl);
//		 loadCsses(en, cssUrls, csssName, indicatorStr,cssReceiver);
//	 }
//	 
//	 public final native void loadCsses(NativeUtils en,JsArrayString cssUrls,String csssName,String indicatorStr,IwantLoadScripts cssReceiver)/*-{
//	 	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::showIndicator(Ljava/lang/String;)(indicatorStr);
//	    $wnd.Y.Get.css(cssUrls,
//		  {
//		    onSuccess: function(o) {
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::hideIndicator()();
//				en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadScripts;Z)(cssReceiver,true);
//		    },
//		    onFailure: function(o) {
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadScripts;Z)(cssReceiver,false);
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::hideIndicator()();
//		      $wnd.alert(csssName + "加载失败！");
//		    },
//		    onTimeout: function(o) {
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadScripts;Z)(cssReceiver,false);
//		    	en.@com.m3958.firstgwt.client.nativemethod.NativeUtils::hideIndicator()();
//		      $wnd.alert(csssName + "加载超时！");
//		    },
//		    data: "foo",
//		    timeout: 10000, // 10 second timeout
//		    insertBefore:'styleoverride'
//		  });
//	}-*/;
	 
	 private Map<String, Boolean> scriptStatus = new HashMap<String, Boolean>();
	 
	 public void putScriptStatus(String key,boolean b){
		 scriptStatus.put(key, b);
	 }
	 
	 public boolean getScriptStatus(String key){
		 if(scriptStatus.containsKey(key)){
			 return scriptStatus.get(key);
		 }else{
			 return false;
		 }
	 }
	 public final native void addMessageClickListener()/*-{
	 		$wnd.Y.delegate('click',function(e){
				e.preventDefault();
				var uuid = this.get('id');
				(function(){
					function changeLoginStatus(){
						if(ns.logined){
							loginBar.setContent('<span>' + ns.user.loginName +  '</span> <a href="/openid.html#tab=3">修改</a> | <a href="/notrpclogin?logout=true">登出</a>');
						}
					}
					var tH = {
							success: function(id, o, args) {
									   var res = $wnd.Y.JSON.parse(o.responseText);
									   if(res.response.status == 0){
										   alert("成功");
									   }else{
										   alert('失败！');
									   }
									 },
							failure: function(id, o, args) {
										alert("发生未知故障！");
									 }
	
						};
					var cfg = {
							method:'POST',
							data:'uuid=' + uuid,
							on: {
								success: tH.success,
								failure: tH.failure
							},
							context: tH
						};
					
					$wnd.Y.io('/tokenconsume', cfg);		
				})();
		},'body','a.m3958-confirm-token');
	}-*/;
	 
//	public final native void initKe()/*-{
//		$wnd.KE.init({
//			id : 'content1',
//			width:'100%',
//			height:'600px',
//			cssPath:$wnd.firstgwtns.getCurrentWebSite().richEditorCssPath,
//			items : ['source', '|', 'fullscreen', 'undo', 'redo', 'print', 'cut', 'copy', 'paste',
//		      			'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
//		      			'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
//		      			'superscript', '|', 'selectall', '-',
//		      			'title', 'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold',
//		      			'italic', 'underline', 'strikethrough', 'removeformat', '|','flash', 'media', 'advtable', 'hr', 'emoticons', 'link', 'unlink'],//, '|', 'about','|','my_image'
//			afterCreate : function(id) {
//					$wnd.KE.util.focus(id);
//				}
//			});
//			$wnd.KE.create('content1');
//	}-*/;

	
//	public  final native void hideKindeditorIframe()/*-{
//		$wnd.Y.all('iframe.ke-iframe').each(function(node){
//			node.hide();
//		});
//	}-*/;
//	
//	public  final native void showKindeditorIframe()/*-{
//		$wnd.Y.all('iframe.ke-iframe').each(function(node){
//			node.show();
//		});
//	}-*/;
	
	public  final native void fetchScript(String url)/*-{
		var script = $doc.createElement("script");
	   	script.setAttribute("src", url);
	   	script.setAttribute("type", "text/javascript");
	   	$doc.body.appendChild(script);
	}-*/;
	
	public  final native String getKeContent(String id)/*-{
		return $wnd.KE.html(id);
	}-*/;
	
	public  final native void setKeContent(String id,String content)/*-{
		$wnd.Y.log(id);
		$wnd.Y.log(content);
		$wnd.KE.html(id,content);
	}-*/;
	
	
	
	public  final native void redirect(String url)/*-{
		$wnd.location = url;
	}-*/;
	
	public  final native void reload()/*-{
		$wnd.location.reload();
	}-*/;
	
	
	public  final native void redirectToLogin()/*-{
		if($wnd.location.href.indexOf("codesvr") == -1){
			$wnd.location = "/Login.html";
		}else{
			$wnd.location = "http://127.0.0.1:8888/Login.html?gwt.codesvr=127.0.0.1:9997";
		}
	}-*/;
	
	public  final native void redirectToHomePage()/*-{
		if($wnd.location.href.indexOf("codesvr") == -1){
			$wnd.location = "/";
		}else{
			$wnd.location = "http://127.0.0.1:8888/?gwt.codesvr=127.0.0.1:9997";
		}
	}-*/;
	
	public  final native String getTimeZone()/*-{
		var timezone = $wnd.jzTimezoneDetector.determine_timezone().timezone;
		return timezone.olson_tz;
	}-*/;
}
